<?php 
/*
 * 公共控制器
 * */
class CommonController extends Controller{
	/*
	 * 递归获得父级
	 * */
	 private function _getFatherCate($data,$cid){
	 	$temp = array();
		foreach($data as $v){
			if($v['cid']==$cid){
				$temp[]=$v;
				$temp=array_merge($temp,$this->_getFatherCate($data, $v['pid']));
			}
		}
		return $temp;
	 }
	 
	 protected function findFather(){
	 	//获取cid
		$cid=isset($_GET['cid']) ? (int)$_GET['cid']:0;
	 	//其实就是找父级的过程，需要用递归，因为不确定有多少父级
		$data=M()->query("SELECT * FROM hd_category");
		$fatherCate=$this->_getFatherCate($data,$cid);
		//数组反转
		$fatherCate=array_reverse($fatherCate);
		$this->assign('fatherCate',$fatherCate);
		return $cid;
	 }
	/*
	 * 获取顶级分类
	 * */
	protected function topCate(){
		$topCate=M()->query("SELECT * FROM hd_category WHERE pid=0");
		$this->assign('topCate',$topCate);
		
		$topC=M()->query("SELECT count(*) c FROM hd_ask");
		//p($topC);die;
		$this->assign('topC',$topC);
	}
	
	/*
	 * 经验转换等级
	 * */
	protected function expToLv($exp){
		//获取等级对应经验的数据
		$lvData = include "Application/Common/Conf/lvConfig.php";
		foreach ($lvData as $lv => $e){
			if($exp<$e){
				return $lv - 1;
			}
		}
		return 20;
	}
	/**
	 * 等级转换为经验
	 */
	protected function lvToExp($lv){
		$lvData = include "Application/Common/Conf/lvConfig.php";
		return $lvData[$lv];
	}
	/*
	 * 每日回答问题最多的人
	 * */
	protected function dayStar(){
		//获得每天零点的时间戳
		$zero=strtotime(date('Y-m-d'));
		//统计 排序  截取  分组
		$sql = "SELECT COUNT(*) AS c,uid FROM hd_answer WHERE time>={$zero} GROUP BY uid ORDER BY c DESC LIMIT 1";
		//每日回答之星有可能没有
		$data=M()->query($sql);
		return $data;
	}
	/*
	 * 历史回答最多的人
	 * */
	 protected function historyStar(){
	 	//直接查询hd_user,按照回答降序排序截取1个
	 	$historyArr=M()->query("SELECT * FROM hd_user ORDER BY answer DESC LIMIT 1");
		//p($historyStar);exit;
		return $historyArr;
		
	 }
	
	
	
	/**
	 * 后盾问答助人光荣榜
	 */
	protected function helpPeople(){
		//直接查询hd_user,按照采纳数降序排序截取1个
			//直接查询hd_user,按照采纳数降序排序截取1个
		$helpArr=M()->query("SELECT * FROM hd_user ORDER BY accept DESC LIMIT 3");
		return $helpArr;
	}
		/*
		 * 个人中心信息
		 * */
	protected function memberLogin(){
		//登陆以后用户的数据处理	
		//sessiond的用户名和密码等于get传过来的值	
		$uid=isset($_GET['uid']) ? ($_GET['uid']) : 0;
//		if(isset($_SESSION['uid']) && isset($_SESSION['uname'])){
			//查询user表中所有的uid
			$sql="SELECT * FROM hd_user WHERE uid={$uid}";
			$userData=M()->query($sql);
			//判断分配出来的uid的回答不等于0的时候
			if($userData[0]['answer']!=0){
				//采纳率=它的采纳数/它的回答数*100
				$userData[0]['cnl']=$userData[0]['accept']/$userData[0]['answer'] *100 ;
				//p($userData[0]['cnl']);die;
				//截取两位数，传第二个参数就是小数点后几位
				$userData[0]['cnl']=round($userData[0]['cnl'],2) . "%";
			}else{
				//反之为0
				$userData[0]['cnl']=0;
			}
			//等级等于经验转等级
			$userData[0]['lv']=$this->expToLv($userData[0]['exp']);
			
			//p($userData);
			//分配出去
			$this->assign('userData',$userData[0]);
			
//		}
	}
	
	
	
	
	
	
	
	
	
	
	
	
}

































 ?>